import taosrest

def check_syslog_insertion():
    try:
        conn = taosrest.connect(
            url="http://192.168.1.251:6041",
            user="root",
            password="taosdata"
        )
        cursor = conn.cursor()

        # 查询 syslogdb 下的所有表
        cursor.execute("SELECT table_name FROM information_schema.ins_tables WHERE db_name = 'syslogdb'")
        tables = cursor.fetchall()
        if not tables:
            print("❌ syslogdb 数据库里没有任何表！可能没有收到任何Syslog。")
            return

        print(f"✅ syslogdb数据库中的子表数量：{len(tables)}")
        for table_info in tables:
            table_name = table_info[0]
            print(f"正在检查表：{table_name}")

            # 查询每张表最近5条数据，包括 log_level
            cursor.execute(f"SELECT ts, source_ip, log_level, full_message FROM syslogdb.{table_name} ORDER BY ts DESC LIMIT 5")
            rows = cursor.fetchall()
            if rows:
                print(f"  ➡️ 最近收到的日志：")
                for row in rows:
                    print(f"    时间: {row[0]} | IP: {row[1]} | Level: {row[2]} | 日志: {row[3]}")
            else:
                print(f"  ⚠️  表 {table_name} 中没有数据。")

    except Exception as e:
        print(f"连接或查询失败: {e}")
    finally:
        if 'conn' in locals():
            conn.close()

if __name__ == "__main__":
    check_syslog_insertion()